package com.wangjiacai.utils.utils;

import com.wangjiacai.utils.output.ChineseWord;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChineseUtil {

    public static int countChinese(String input){
        Pattern pattern = Pattern.compile("[^\u4E00-\u9FA5]+");
        Matcher matcher=pattern.matcher(input);
        return matcher.replaceAll("").length();
    }

    public static List<ChineseWord> matcherChinese(String input){
        Pattern pattern = Pattern.compile("([\u4E00-\u9FA5]*)([^\u4E00-\u9FA5]*)");
        Matcher matcher=pattern.matcher(input);
        List<ChineseWord> list=new ArrayList<>();
        while (matcher.find()){
            String chinese=matcher.group(1);
            String noChinese=matcher.group(2);
            if(!chinese.trim().equals("")){
                list.add(new ChineseWord(input,chinese,matcher.start(),matcher.start()+chinese.length(),true));
            }
            if(!noChinese.equals("")){
                list.add(new ChineseWord(input,noChinese,matcher.start()+chinese.length(),matcher.end(),false));
            }
        }
        return list;
    }

}
